<template>
<el-dialog :title="!dataForm.attrGroupId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
  <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="组名" prop="attrGroupName">
      <el-input v-model="dataForm.attrGroupName" placeholder="请输入组名"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="请输入排序"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="请输入简要描述信息"></el-input>
    </el-form-item>
    <el-form-item label="分组图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="请选择图标"></el-input>
    </el-form-item>
    <el-form-item label="所属分类" prop="catelogId">
      <div class="block">
        <el-cascader style="width:45%" filterable v-model="value" :options="options" separator="-" placeholder="请选择所属分类" :props="props" @change="handleChange" ref="categoryOptions"></el-cascader>
      </div>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取消</el-button>
    <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  </span>
</el-dialog>
</template>

<script>
export default {
  data() {
    return {
      value: [],
      options: [],
      props: {
        expandTrigger: 'hover',
        value: 'catId',
        label: 'name',
        children: 'subList'
      },
      visible: false,
      dataForm: {
        attrGroupId: 0,
        attrGroupName: '',
        sort: '',
        descript: '',
        icon: '',
        catelogId: ''
      },
      dataRule: {
        attrGroupName: [{
          required: true,
          message: '${column.comments}不能为空',
          trigger: 'blur'
        }],
        sort: [{
          required: true,
          message: '${column.comments}不能为空',
          trigger: 'blur'
        }],
        descript: [{
          required: true,
          message: '${column.comments}不能为空',
          trigger: 'blur'
        }],
        icon: [{
          required: true,
          message: '${column.comments}不能为空',
          trigger: 'blur'
        }],
        catelogId: [{
          required: true,
          message: '${column.comments}不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init(id, catelogId) {
      this.dataForm.attrGroupId = id || 0
      this.dataForm.catelogId = catelogId
      this.value = catelogId
      console.log("接收到的catelogId:", catelogId)
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getDataList()
        this.getDetail(this.dataForm.attrGroupId)
      })
    },

    //获取分类数据信息
    getDataList() {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({
        //解构函数
        data
      }) => {
        this.options = data.data
      })
    },

    getDetail(id) {
      if (this.dataForm.attrGroupId) {
        this.$http({
          url: this.$http.adornUrl(`/product/attrgroup/info/${this.dataForm.attrGroupId}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataForm.attrGroupName = data.attrGroup.attrGroupName
            this.dataForm.sort = data.attrGroup.sort
            this.dataForm.descript = data.attrGroup.descript
            this.dataForm.icon = data.attrGroup.icon
            this.dataForm.catelogId = data.attrGroup.catelogId
          }
        })
      }
    },

    handleChange() {
      let checkedData = this.$refs.categoryOptions.panel.getCheckedNodes()
      console.log("触发了选中项变动的事件监听----->>>>>>", checkedData)
      this.dataForm.catelogId = checkedData[0].data.catId
      console.log("checkedData:", this.dataForm.catelogId)
    },

    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/attrgroup/${!this.dataForm.attrGroupId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'attrGroupId': this.dataForm.attrGroupId || undefined,
              'attrGroupName': this.dataForm.attrGroupName,
              'sort': this.dataForm.sort,
              'descript': this.dataForm.descript,
              'icon': this.dataForm.icon,
              'catelogId': this.dataForm.catelogId
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
